package com.ljh.account.controller;

import com.github.pagehelper.PageInfo;
import com.ljh.account.dao.Income;
import com.ljh.account.dao.Resp.IncomeResp;
import com.ljh.account.dao.Resp.Page;
import com.ljh.account.service.IncomeService;
import com.ljh.account.utils.Response;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/protected")
public class IncomeController {

    @Autowired
    private IncomeService incomeService;


    @PostMapping("/income")
    public Response income(@RequestBody Income income, HttpServletRequest request){
        Object userId = request.getAttribute("userId");
        income.setUserId((Long) userId);
        incomeService.insertIncome(income);
        return Response.success("新增记录成功！");
    }

    @GetMapping("/income")
    public  Response income(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer bankAccountId,
            @RequestParam(required = false) BigDecimal minAmount,
            @RequestParam(required = false) BigDecimal maxAmount,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request){
        Object userId = request.getAttribute("userId");
        PageInfo list = incomeService.getIncomeWithFilter((Long)userId, categoryId, bankAccountId, minAmount, maxAmount,page,size);
        return Response.success(list);
    }

    @DeleteMapping("/income")
    public Response income(@RequestParam Long id){
        incomeService.deleteIncome(id);
        return Response.success("删除成功");
    }
}
